// pages/Tabbar/My/realName/realName.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;

import { userRealNameAuth } from '../../../../api/user';

Page({
  stopTouchMove(){
    return false;
  },
  getInputValue(e){
    var text = e.detail.value;
    this.setData({
      real_name_value:text,
    });
  },
  idcardInputValue(e){
    var text = e.detail.value;
    this.setData({
      idcard_value:text,
    });
  },
  // 实名认证选择
  selectAuthType(e){
    let index = e.currentTarget.dataset.index;
    var list = this.data.auth_datas;
    for (var i in list) {
      var item = list[i];
      if(item.index == index){
          item.isSelect = true;
      }else{
          item.isSelect = false;
      }
    }
    this.setData({
      selectIndex:index,
      auth_datas:list,
    });
  },
  // 提交实名
  commitAuth(){
    if(this.data.real_name_value.length==0){ wx.showToast({title:"请输入真实姓名",icon:"none"});return;}
    if(this.data.idcard_value.length==0){ wx.showToast({title:"请输入身份证号",icon:"none"});return;}
    if(this.data.selectIndex<0){wx.showToast({title:"请选择认证方式",icon:"none" });return;}
    const params = {
      'realName': this.data.real_name_value,
      'idCard': this.data.idcard_value,
      'authType': this.data.authTypeList[this.data.selectIndex]
    }
    userRealNameAuth(params)
    .then(res => {
      if(res.success){
        wx.setStorageSync('hasNewUser', 'true')
        // if(this.data.selectIndex == 0){
        //   wx.navigateToMiniProgram({
        //     appId: 'wx1cf2708c2de46337',  // 公证签小程序APPID
        //     path: `/pages/face/index?bizToken=${res.data.authUrl}`, // 刷脸页面地址
        //   })
        // }else{
        //   wx.navigateTo({
        //     url: '/pages/link/link/link?url=' + encodeURIComponent(res.data.authUrl) + '&title=' + JSON.stringify(this.data.auth_datas[this.data.selectIndex].title),
        //   })
        // }
        wx.navigateTo({
          url: '/pages/link/link/link?url=' + encodeURIComponent(res.data.authUrl) + '&title=' + JSON.stringify(this.data.auth_datas[this.data.selectIndex].title),
        })
      }else{
        if(res.errCode == 'USER_ALREADY_AUTH'){
          wx.setStorageSync('hasNewUser', 'true')
          wx.showToast({
            title: '当前账号已实名',
            icon: 'none',
          })
        }else{
          wx.showToast({
            title: res.errMessage,
            icon: 'none',
          })
        }
      }
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    windowH:wx.getWindowInfo().screenHeight-wx.getWindowInfo().statusBarHeight-44-10,
    selectIndex : 0,
    // 姓名
    real_name_value:wx.getStorageSync('realName') ?? '',
    // 身份证号
    idcard_value:wx.getStorageSync('userIdCard') ?? '',
    realNameStatus: wx.getStorageSync('realNameStatus') ?? 0,
    auth_datas:[
      {"icon":assetsAddress+"/mine/face_auth.png","title":"人脸识别","des":"支持腾讯云以及支付宝人脸识别","index":0,"radio_icon":assetsAddress+"/mine/auth_icon.png","radio_select_icon":assetsAddress+"/mine/auth_select_icon.png","isSelect":true},
      {"icon":assetsAddress+"/mine/bank_auth.png","title":"银行卡认证","des":"需保证银行卡预留手机号可以收到短信验证码","index":1,"radio_icon":assetsAddress+"/mine/auth_icon.png","radio_select_icon":assetsAddress+"/mine/auth_select_icon.png","isSelect":false},
      {"icon":assetsAddress+"/mine/phone_auth.png","title":"手机号认证","des":"需保证手机号办理时的身份证信息为本人","index":2,"radio_icon":assetsAddress+"/mine/auth_icon.png","radio_select_icon":assetsAddress+"/mine/auth_select_icon.png","isSelect":false}
    ],
    authTypeList: ['PSN_FACEAUTH_BYURL','PSN_BANK4_AUTHCODE','PSN_TELECOM_AUTHCODE'],
    shareTitle: app.globalData.global.shareTitle,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(wx.getStorageSync('realName'),'realname')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let userId = wx.getStorageSync('userId')
    return {
      title: this.data.shareTitle,
      path: '/pages/Tabbar/Home/Home?scene='+userId,
    }
  }
})